package com.world.rickcloudendpoint.fusion;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: jlsong
 * @date: 2022/03/06 21:57:48
 * @version: 1.0
 */
@Component
public class ThreeAFusion {

    final RedisTemplate<String, Object> redisTemplate;

    final MongoTemplate mongoTemplate;

    public ThreeAFusion(RedisTemplate<String, Object> redisTemplate, MongoTemplate mongoTemplate) {
        this.redisTemplate = redisTemplate;
        this.mongoTemplate = mongoTemplate;
    }

    public void process() {
        Map<Integer, List<AisMsg>> batchAisMap = new HashMap<>(16);
        Map<String, List<TwoMsg>> twoMsgMap = new HashMap<>(16);
        Object message = redisTemplate.opsForList().rightPop("message");
        if (message != null) {
            MsgParseObj msgParseObj = parseMessage(message);
            if (msgParseObj == null) {
                return;
            }
            // 进行match
            if (msgParseObj.getAisMsgList() != null && msgParseObj.getAisMsgList().size() > 0) {
                Map<Integer, List<AisMsg>> map = matchAis(msgParseObj.getAisMsgList());
                batchAisMap.putAll(map);
            }
            if (msgParseObj.getAcarsMsgList() != null && msgParseObj.getAcarsMsgList().size() > 0){
                Map<String, List<TwoMsg>> map = matchAcars(msgParseObj.getAcarsMsgList());
//              Map<String, AcarsMsg> acarsMsgMap =  msgParseObj.getAcarsMsgList().stream().collect(Collectors.toMap(AcarsMsg::getFlightNo, AcarsMsg->AcarsMsg));
              twoMsgMap.putAll(map);
            }
            if (msgParseObj.getAdsbMsgList() != null && msgParseObj.getAdsbMsgList().size() > 0){
                Map<String, List<TwoMsg>> map = matchAdsb(msgParseObj.getAdsbMsgList());
                twoMsgMap.putAll(map);
            }
        }
        if (batchAisMap.size() > 0){
             for (Map.Entry<Integer, List<AisMsg>> entry : batchAisMap.entrySet()){
                 Integer imoNo = entry.getKey();
                 // 判断航迹是否存在
                 // 不存在，则是新的航迹，创建航迹，聚合所有字段
                 String trackId  = null;
                 if (trackId != null){
                     List<AisMsg> aisMsgList = entry.getValue();
                     
                 }

             }
        }
        if (twoMsgMap.size() > 0){

        }
    }

    private Map<String, List<TwoMsg>> matchAdsb(List<AdsbMsg> adsbMsgList) {
        Map<String, List<TwoMsg>> result = new HashMap<>(16);
        for (int i = 0; i < adsbMsgList.size(); i++){
            String flightNo = adsbMsgList.get(i).getFlightNo();
            List<TwoMsg> tmpList = result.get(flightNo);
            if (tmpList == null){
                tmpList = new ArrayList<>();
            }
            tmpList.add(adsbMsgList.get(i));
            result.put(flightNo, tmpList);
        }
        return result;
    }

    private Map<String, List<TwoMsg>> matchAcars(List<AcarsMsg> acarsMsgList) {
        Map<String, List<TwoMsg>> result = new HashMap<>(16);
        for (int i = 0; i < acarsMsgList.size(); i++){
            String flightNo = acarsMsgList.get(i).getFlightNo();
            List<TwoMsg> tmpList = result.get(flightNo);
            if (tmpList == null){
                tmpList = new ArrayList<>();
            }
            tmpList.add(acarsMsgList.get(i));
            result.put(flightNo, tmpList);
        }
        return result;
    }

    private Map<Integer, List<AisMsg>> matchAis(List<AisMsg> aisMsgList) {
        Map<Integer, List<AisMsg>> result = new HashMap<>(16);
        for (int i = 0; i < aisMsgList.size(); i++){
            Integer imoNo = aisMsgList.get(i).getImoNo();
            List<AisMsg> tmpList = result.get(imoNo);
            if (tmpList == null){
                tmpList = new ArrayList<>();
            }
            tmpList.add(aisMsgList.get(i));
            result.put(imoNo, tmpList);
        }
        return result;
    }

    private MsgParseObj parseMessage(Object message) {
        int num = 5;
        List<AisMsg> aisMsgList = new ArrayList<>();
        List<AcarsMsg> acarsMsgList = new ArrayList<>();
        List<AdsbMsg> adsbMsgList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            aisMsgList.add(new AisMsg());
        }
        for (int i = 0; i < num; i++) {
            acarsMsgList.add(new AcarsMsg());
        }
        for (int i = 0; i < num; i++) {
            adsbMsgList.add(new AdsbMsg());
        }
        MsgParseObj obj = new MsgParseObj();
        obj.setAisMsgList(aisMsgList);
        obj.setAcarsMsgList(acarsMsgList);
        obj.setAdsbMsgList(adsbMsgList);
        return obj;
    }
}
